package com.fh.service.impl;

import com.fh.mapper.TOrderMapper;
import com.fh.model.TOrder;
import com.fh.model.TOrderinfo;
import com.fh.model.TProduct;
import com.fh.model.dto.OrderInfoDTO;
import com.fh.service.OrderInfoService;
import com.fh.service.OrderService;
import com.fh.service.ProductService;
import com.fh.util.DateUtil;
import com.fh.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class OrderServiceImpl implements OrderService {
    @Resource
    private TOrderMapper tOrderMapper;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ProductService productService;

    public void addOrder(OrderInfoVo orderInfoVo) {
        TOrder order1 = new TOrder();
        order1.setUserId(orderInfoVo.getUserId());
        order1.setOrderId(DateUtil.getNumberId());
        order1.setCreatedTime(new Date());
        order1.setOrderAddr(orderInfoVo.getOrder_addr());
        order1.setOrderUser(orderInfoVo.getOrder_user());
        order1.setOrderTel(orderInfoVo.getOrder_tel());
        order1.setOrderFlag((short)1);
        order1.setOrderPrice(orderInfoVo.getTotalPrice());
        tOrderMapper.insert(order1);
        List<TOrderinfo> orderinfoList = new ArrayList<>();
        for (int i = 0; i <orderInfoVo.getPids().size() ; i++) {
            TOrderinfo orderinfo = new TOrderinfo();
            orderinfo.setOrderId(order1.getOrderId());
            orderinfo.setPid(orderInfoVo.getPids().get(i));
            orderinfoList.add(orderinfo);
        }
        orderInfoService.addOrderInfoService(orderinfoList);
    }// 封装订单详情数据


    public List<OrderInfoDTO> queryOrderList(Long userId) {
        //根据用户id去订单表中查询该用户的订单
        List<TOrder>orderList=  tOrderMapper.queryOrderByUserId(userId);
       //封装OrderInfoDTO集合
        List<OrderInfoDTO>orderInfoDTOLIst= new ArrayList<OrderInfoDTO>();
        //2.根据订单id去订单详情表中查询商品的id集合

        orderList.forEach(order->{
            List<TOrderinfo>orderInfoList= orderInfoService.queryProductByOrderId(order.getOrderId());
             //封装order信息
            OrderInfoDTO orderInfoDTO=new OrderInfoDTO();
            orderInfoDTO.setCreateDate(order.getCreatedTime());
            orderInfoDTO.setOrderNum(order.getOrderId());
            orderInfoDTO.setOrderSatus(order.getOrderFlag());
            orderInfoDTO.setTotalPrice(order.getOrderPrice());


            //根据商品id查询商品信息
            List<TProduct>productList=new ArrayList<>();
            orderInfoList.forEach(orderinfo->{
                TProduct product= productService.queryProductListById(orderinfo.getPid());
                productList.add(product);
            });
            //将查询出的商品集合放到orderInfoDTO中
            orderInfoDTO.setProductList(productList);
            //将订单信息和商品信息放到
            orderInfoDTOLIst.add(orderInfoDTO);
        });

        return orderInfoDTOLIst;
    }

}
